﻿;/////////////////////////////////////////////////////////////;
;                                                             ;
;                 SimpleDX11 Camera Procedures                ;
;                                                             ;
;                                                             ;
;                   (c) 2013 SimpleDX Team                    ;
;                                                             ;
;                         License: MIT                        ;
;                                                             ;
;/////////////////////////////////////////////////////////////;


;-//    Private structures    //
Structure CameraConstantBuffer
  Position.XMFLOAT3
  padding.f
EndStructure

Structure Camera_Template
  *vTable
  *p.Camera
  
  *SD3D.Screen3D
  
  Position.XMFLOAT3
  Target.XMFLOAT3
  Up.XMFLOAT3
  Rotation.XMFLOAT3  
  Aspect.f  
  
  ViewMatrix.D3DMATRIX 
  WorldMatrix.D3DMATRIX
  Projection.D3DMATRIX
EndStructure


;-//    Constructor    //
Procedure New_Camera(*SD3D.Screen3D)
  Protected *this.Camera_Template
 
  *this = AllocateMemory(SizeOf(Camera_Template))
  InitializeStructure(*this, Camera_Template)
  
  If *this
    *this\vTable  = ?Camera_Class    
    *this\p       = *this
    *this\SD3D     = *SD3D
    
    *this\Aspect = ScreenWidth() / ScreenHeight()
   
    *this\Target\x = 0.0
    *this\Target\y = 0.0
    *this\Target\z = 1.0
    
    *this\Up\x = 0.0
    *this\Up\y = 1.0
    *this\Up\z = 0.0
    
    *this\Position\x = 0.0
    *this\Position\y = 0.0
    *this\Position\z = 0.5 
    
;      *this\D3D\GetWorldMatrix(@*this\WorldMatrix)
;      *this\D3D\GetProjectionMatrix(@*this\Projection)
  
    ProcedureReturn *this
  EndIf  
  ProcedureReturn #False
EndProcedure



;-//    Public procedures    //
Procedure Camera_SetPosition(*this.Camera_Template, x.f, y.f, z.f)
  *this\Position\x = x
  *this\Position\y = y
  *this\Position\z = z
EndProcedure

Procedure Camera_GetPosition(*this.Camera_Template)
  ProcedureReturn *this\Position
EndProcedure


Procedure Camera_SetRotation(*this.Camera_Template, x.f, y.f, z.f)
  *this\Rotation\x = x
  *this\Rotation\y = y
  *this\Rotation\z = z
EndProcedure

Procedure Camera_GetRotation(*this.Camera_Template)
  ProcedureReturn *this\Rotation
EndProcedure

Procedure Camera_SetTarget(*this.Camera_Template, x.f, y.f, z.f)
  *this\Target\x = x
  *this\Target\y = y
  *this\Target\z = z
EndProcedure

Procedure Camera_GetTarget(*this.Camera_Template)
  ProcedureReturn *this\Target
EndProcedure

Procedure Camera_GetViewMatrix(*this.Camera_Template)
  ProcedureReturn *this\ViewMatrix
EndProcedure

Procedure Camera_Render(*this.Camera_Template)  
  
  Protected rMatrix.D3DMATRIX, Pos.XMFLOAT3, Tar.XMFLOAT3, Up.XMFLOAT3
 
  
  Pos\x = *this\Position\x
  Pos\y = *this\Position\y
  Pos\z = *this\Position\z
  Tar\x = *this\Target\x
  Tar\y = *this\Target\y
  Tar\z = *this\Target\z  
  Up\x = *this\Up\x
  Up\y = *this\Up\y
  Up\z = *this\Up\z  
  
	Protected Pitch.f = *this\Rotation\x * 0.0174532925   
	Protected Yaw.f   = *this\Rotation\y * 0.0174532925 
	Protected Roll.f  = *this\Rotation\z * 0.0174532925 
	
	D3DXMatrixRotationYawPitchRoll(rMatrix, Yaw, Pitch, Roll)
	
	D3DXVec3TransformCoord(Tar, Tar, @rMatrix)
	D3DXVec3TransformCoord(Up, Up, @rMatrix)
	
	Tar\x + Pos\x
	Tar\y + Pos\y
	Tar\z + Pos\z

  D3DXMatrixLookAtLH(*this\ViewMatrix, Pos, Tar, Up)  
   
EndProcedure

Procedure Camera_Release(*this.Camera_Template)
  FreeMemory(*this)  
EndProcedure


;-//    Private procedures    //

;/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
;/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
;/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
;/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
;/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
;-//    DataSection    //
DataSection
  Camera_Class:
  Data.i @Camera_SetPosition()
  Data.i @Camera_GetPosition()
  Data.i @Camera_SetRotation()
  Data.i @Camera_GetRotation()
  Data.i @Camera_SetTarget()
  Data.i @Camera_GetTarget()
  Data.i @Camera_GetViewMatrix()
  Data.i @Camera_Render()
  Data.i @Camera_Release()
EndDataSection















; IDE Options = PureBasic 5.20 beta 14 LTS (Windows - x64)
; CursorPosition = 127
; FirstLine = 85
; Folding = --
; EnableUnicode
; EnableXP